package org.example.demo.dao;


import com.kfyang.manualgear.annotation.Dao;
import com.kfyang.manualgear.annotation.crud.*;
import org.example.demo.entity.UserEntity;

import java.util.List;

@Dao
public interface UserDao {
    // 查询方法
//    @Query(col = {"id", "name"}, value = "from `user` where name = {name0}")
//    @Construct("constructor1") // 所使用的构造器
    UserEntity selectUserByName(String name0);

    // value内的大括号表示引用方法参数的字段，中括号表示所引用的Frag
    @Query(col = {"id", "name", "create_time"}, value = "from `user` where 1 = 1 [idFrag] [nameFrag]")
    // 片段，cond里为真时才在原sql中插入value中的值
    @Frag(id = "idFrag", value = " and id = {u.id}", cond = "{u.id} != null")
    @Frag(id = "nameFrag", value = " and name = {u.name}", cond = "!{u.name}.isEmpty()")
    @ResultMap({"id", "name", "createTime"}) // 填充UserEntity对应字段
    List<UserEntity> selectUserByNameOrId(@Param("u") UserEntity user);

    // 修改方法
    @Modify("update `user` set name = {user.name} where id = {user.id}")
    int updateUserById(UserEntity user);

    @Modify("delete from `user` where id in {{idList}}")
    int deleteByIds(List<Integer> idList);

}


